package com.xuxin.baby.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.entity.LocalMedia;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UploadFilesBean;
import com.xuxin.baby.contract.ChildSmellyRecordContract;
import com.xuxin.baby.contract.UploadFilesContract;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.photo.PhotoUtils;
import com.xuxin.baby.popup.SmellyPopup;
import com.xuxin.baby.presenter.ChildSmellyRecordPresenter;
import com.xuxin.baby.presenter.UploadFilesPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.PathUtils;
import com.xuxin.baby.view.MyRadioGroup;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020.08.11 0011
 * 记臭臭
 */
public class ChildSmellyRecordActivity extends BaseActivity implements ChildSmellyRecordContract.View, UploadFilesContract.View {
    private final ChildSmellyRecordContract.Presenter presenter = new ChildSmellyRecordPresenter(this);
    private final UploadFilesContract.Presenter presenter2 = new UploadFilesPresenter(this);
    RequestOptions options = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10));
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.child_smelly_image)
    ImageView child_smelly_image;
    @BindView(R.id.child_smelly_record_name)
    TextView child_smelly_record_name;
    @BindView(R.id.child_smelly_record_color)
    MyRadioGroup child_smelly_record_color;
    @BindView(R.id.child_smelly_record_character)
    MyRadioGroup child_smelly_record_character;
    @BindView(R.id.child_smelly_record_freq)
    EditText child_smelly_record_freq;
    @BindView(R.id.child_smelly_record_content)
    EditText child_smelly_record_content;
    @BindView(R.id.child_smelly_record_image)
    ImageView child_smelly_record_image;
    @BindView(R.id.child_smelly_record_ShitQty)
    MyRadioGroup child_smelly_record_ShitQty;

    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID

    private int childSmelly;
    private String eventContent;//内容
    private String shitColor;//颜色
    private String shitCharacter;//性状
    private String shitFreq;//次数
    private String picture;//图片
    private String ShitQty;//便量
    private boolean detail;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_child_smelly_record);
    }

    @Override
    public void initView() {
        button.setText("保存");
        title_right.setImageResource(R.mipmap.img_question);
        title_right.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        childSmelly = intent.getIntExtra(BaseConstant.childSmelly, 0);
        title_name.setText((childSmelly == 0) ? "记臭臭" : "臭臭评测");
        child_smelly_record_color.setOnCheckedChangeListener(new onColorListener());
        child_smelly_record_character.setOnCheckedChangeListener(new onCharacterListener());
        child_smelly_record_ShitQty.setOnCheckedChangeListener(new onShitQtyListener());
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
        detail = getIntent().getBooleanExtra("detail", false);
        if (detail) {
            button.setVisibility(View.GONE);
            shitColor = getIntent().getStringExtra("shitColor");
            shitCharacter = getIntent().getStringExtra("shitCharacter");
            ShitQty = getIntent().getStringExtra("ShitQty");
            shitFreq = getIntent().getStringExtra("shitFreq");
            eventContent = getIntent().getStringExtra("eventContent");
            picture = getIntent().getStringExtra("picture");
            child_smelly_record_name.setText(shitCharacter);
            child_smelly_record_freq.setText(shitFreq);
            child_smelly_record_content.setText(eventContent);
            LogUtils.e("shitColor = " + shitColor);
            LogUtils.e("shitCharacter = " + shitCharacter);
            LogUtils.e("ShitQty = " + ShitQty);
            if ("灰白".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color1);
            } else if ("绿色".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color2);
            } else if ("黄色".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color3);
            } else if ("棕色".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color4);
            } else if ("黑褐色".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color5);
            } else if ("红色".equals(shitColor)) {
                child_smelly_record_color.check(R.id.child_smelly_record_color6);
            }
            if (!StringUtils.isEmpty(picture)) {
                String image = Retrofit.image_url + picture;
                LogUtils.e("记臭臭 image = " + image);
                GlideApp.with(context).load(image).placeholder(R.mipmap.img_load).apply(options).into(child_smelly_record_image);
            }
            if ("一般".equals(shitCharacter)) {
                LogUtils.e("一般");
                child_smelly_record_character.check(R.id.child_smelly_record_character1);
            } else if ("很稀".equals(shitCharacter)) {
                LogUtils.e("很稀");
                child_smelly_record_character.check(R.id.child_smelly_record_character2);
            } else if ("水样".equals(shitCharacter)) {
                LogUtils.e("水样");
                child_smelly_record_character.check(R.id.child_smelly_record_character3);
            } else if ("粘稠".equals(shitCharacter)) {
                LogUtils.e("粘稠");
                child_smelly_record_character.check(R.id.child_smelly_record_character4);
            } else if ("较干".equals(shitCharacter)) {
                LogUtils.e("较干");
                child_smelly_record_character.check(R.id.child_smelly_record_character5);
            } else if ("干硬".equals(shitCharacter)) {
                LogUtils.e("干硬");
                child_smelly_record_character.check(R.id.child_smelly_record_character6);
            }
            if ("少许".equals(ShitQty)) {
                LogUtils.e("少许");
                child_smelly_record_ShitQty.check(R.id.child_smelly_record_ShitQty_1);
            } else if ("小于25%".equals(ShitQty)) {
                LogUtils.e("小于25%");
                child_smelly_record_ShitQty.check(R.id.child_smelly_record_ShitQty_2);
            } else if ("25%-50%".equals(ShitQty)) {
                LogUtils.e("25%-50%");
                child_smelly_record_ShitQty.check(R.id.child_smelly_record_ShitQty_3);
            } else if ("多余50%".equals(ShitQty)) {
                LogUtils.e("多余50%");
                child_smelly_record_ShitQty.check(R.id.child_smelly_record_ShitQty_4);
            }
        } else {
            child_smelly_record_color.check(R.id.child_smelly_record_color1);
            child_smelly_record_character.check(R.id.child_smelly_record_character1);
            child_smelly_record_ShitQty.check(R.id.child_smelly_record_ShitQty_1);
        }
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right, R.id.child_smelly_record_image,
            R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.title_right) {
            showPopup();
        } else if (id == R.id.child_smelly_record_image) {
            if (detail) {
                PhotoUtils.browser(context, picture);
            } else {
                PhotoUtils.openPhoto(this, new PhotoUtils.OnResultListener() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        takePhoto(result);
                    }
                });
            }
        } else if (id == R.id.button) {
            shitFreq = child_smelly_record_freq.getText().toString();
            eventContent = child_smelly_record_content.getText().toString();
            if (StringUtils.isEmpty(shitColor)) {
                ToastUtils.showShort("请选择大便颜色");
                return;
            }
            if (StringUtils.isEmpty(shitCharacter)) {
                ToastUtils.showShort("请选择大便性状");
                return;
            }
            if (StringUtils.isEmpty(shitFreq)) {
                ToastUtils.showShort("请输入次数");
                return;
            }
            if (childSmelly == 0) {
                try {
                    if (null != parts && !parts.isEmpty()) {
                        popup.show();
                        handler.sendEmptyMessage(0);
                    } else {
                        popup.show();
                        handler.sendEmptyMessage(1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismiss();
                }
            } else {
                ActivityUtils.startActivity(EvaluateResultActivity.class);
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0://拍照上传
                    presenter2.uploadFiles(tenantId,
                            token,
                            childId,
                            "Edu_EventShit",
                            "pic1",
                            parts);
                    break;
                case 1://记录
                    presenter.record(tenantId,
                            token,
                            TimeUtils.getNowString(),
                            childId,
                            shitCharacter,
                            eventContent,
                            shitColor,
                            shitCharacter,
                            shitFreq,
                            ShitQty,
                            picture);
                    break;

            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        finish();
    }

    @Override
    public void onSuccess(UploadFilesBean uploadFilesBean) {
        if (!StringUtils.isEmpty(uploadFilesBean.getResult())) {
            picture = uploadFilesBean.getResult();
        }
        handler.sendEmptyMessage(1);
    }

    private List<MultipartBody.Part> parts;

    public void takePhoto(List<LocalMedia> result) {
        LocalMedia media = result.get(0);
        String path = media.isCompressed() ? media.getCompressPath() : media.getPath();
        GlideApp.with(context).load(path).centerCrop().into(child_smelly_record_image);
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(file, MediaType.parse("image/jpeg"));
        parts = new ArrayList<>();
        parts.add(MultipartBody.Part.createFormData("files", PathUtils.getImageName(), requestBody));
    }

    /**
     * 大便颜色
     */
    private class onColorListener implements MyRadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(MyRadioGroup group, int checkedId) {
            if (checkedId == R.id.child_smelly_record_color1) {
                shitColor = "灰白";
                GlideApp.with(context).load(R.mipmap.img_smelly_color1).centerCrop().into(child_smelly_image);
            } else if (checkedId == R.id.child_smelly_record_color2) {
                shitColor = "绿色";
                GlideApp.with(context).load(R.mipmap.img_smelly_color2).centerCrop().into(child_smelly_image);
            } else if (checkedId == R.id.child_smelly_record_color3) {
                shitColor = "黄色";
                GlideApp.with(context).load(R.mipmap.img_smelly_color3).centerCrop().into(child_smelly_image);
            } else if (checkedId == R.id.child_smelly_record_color4) {
                shitColor = "棕色";
                GlideApp.with(context).load(R.mipmap.img_smelly_color4).centerCrop().into(child_smelly_image);
            } else if (checkedId == R.id.child_smelly_record_color5) {
                shitColor = "黑褐色";
                GlideApp.with(context).load(R.mipmap.img_smelly_color5).centerCrop().into(child_smelly_image);
            } else if (checkedId == R.id.child_smelly_record_color6) {
                shitColor = "红色";
                GlideApp.with(context).load(R.mipmap.img_smelly_color6).centerCrop().into(child_smelly_image);
            }
        }
    }

    /**
     * 大便性状
     */
    private class onCharacterListener implements MyRadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(MyRadioGroup group, int checkedId) {
            if (checkedId == R.id.child_smelly_record_character1) {
                shitCharacter = "一般";
                child_smelly_record_name.setText("一般");
            } else if (checkedId == R.id.child_smelly_record_character2) {
                shitCharacter = "很稀";
                child_smelly_record_name.setText("很稀");
            } else if (checkedId == R.id.child_smelly_record_character3) {
                shitCharacter = "水样";
                child_smelly_record_name.setText("水样");
            } else if (checkedId == R.id.child_smelly_record_character4) {
                shitCharacter = "粘稠";
                child_smelly_record_name.setText("粘稠");
            } else if (checkedId == R.id.child_smelly_record_character5) {
                shitCharacter = "较干";
                child_smelly_record_name.setText("较干");
            } else if (checkedId == R.id.child_smelly_record_character6) {
                shitCharacter = "干硬";
                child_smelly_record_name.setText("干硬");
            }
        }
    }

    private class onShitQtyListener implements MyRadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(MyRadioGroup group, int checkedId) {
            if (checkedId == R.id.child_smelly_record_ShitQty_1) {
                ShitQty = "少许";
            } else if (checkedId == R.id.child_smelly_record_ShitQty_2) {
                ShitQty = "小于25%";
            } else if (checkedId == R.id.child_smelly_record_ShitQty_3) {
                ShitQty = "25%-50%";
            } else if (checkedId == R.id.child_smelly_record_ShitQty_4) {
                ShitQty = "多余50%";
            }
        }
    }

    public void showPopup() {
        new XPopup.Builder(context)
                .dismissOnTouchOutside(true)
                .asCustom(new SmellyPopup(context))
                .show();
    }
}